#include "pch.h"
#include <io.h>
#include "../Base/Base.h"
#include "ImportDataCache.h"

ImportDataCache::ImportDataCache()
{
}


ImportDataCache::~ImportDataCache()
{
}

void ImportDataCache::load()
{
	struct _finddata_t fileinfo;

	auto handle = _findfirst("./temp/*.import", &fileinfo);
	if (handle == -1)
		return;
	
	do
	{
		LOG(INFO) << "Read file " << fileinfo.name;

		std::string full_name = "./temp/";
		full_name += fileinfo.name;

		Datas datas;
        Data success_cache_pos;
		readFile(full_name.c_str(), success_cache_pos, datas);

		std::string table_name = std::string(fileinfo.name);
		StringReplace(table_name, ".import", "");

        success_cache_pos_.insert(std::make_pair(table_name, success_cache_pos));
        faild_cache_datas_.insert(std::make_pair(table_name, datas));
	} while (!_findnext(handle, &fileinfo));

	_findclose(handle);
}

bool ImportDataCache::hasCacheData(const char * table)
{
	return faild_cache_datas_.find(table) != faild_cache_datas_.end() || success_cache_pos_.find(table) != success_cache_pos_.end();
}

void ImportDataCache::addFaild(const char * table, Data & data, Data & pos)
{
    if (faild_cache_datas_.find(table) == faild_cache_datas_.end())
    {
        faild_cache_datas_.insert(std::make_pair(table, std::vector<Data>()));
    }

	if (std::find(faild_cache_datas_[table].begin(), faild_cache_datas_[table].end(), data) != faild_cache_datas_[table].end())
	{
		return;
	}

	success_cache_pos_[table] = pos;
	faild_cache_datas_[table].push_back(data);

    saveFile(table, success_cache_pos_[table], faild_cache_datas_[table]);
}

void ImportDataCache::removeFaild(const char * table, Data & data)
{
    if (faild_cache_datas_.find(table) == faild_cache_datas_.end())
    {
        return;
    }

    auto iter = std::find(faild_cache_datas_[table].begin(), faild_cache_datas_[table].end(), data);

    if (iter != faild_cache_datas_[table].end())
    {
        faild_cache_datas_[table].erase(iter);
    }

    saveFile(table, success_cache_pos_[table], faild_cache_datas_[table]);
}

bool ImportDataCache::getFaild(const char * table, int index, Data & data)
{
    if (faild_cache_datas_.find(table) == faild_cache_datas_.end())
    {
        return false;
    }

	if (faild_cache_datas_[table].size() == 0 || index < 0 || index >= faild_cache_datas_[table].size())
	{
		return false;
	}

	data = faild_cache_datas_[table][index];

	return true;
}

int ImportDataCache::countFaild(const char *table)
{
    if (faild_cache_datas_.find(table) == faild_cache_datas_.end())
    {
        return 0;
    }

    return (int)faild_cache_datas_[table].size();
}

void ImportDataCache::addPos(const char *table, Data &pos)
{
    success_cache_pos_[table] = pos;

    saveFile(table, success_cache_pos_[table], faild_cache_datas_[table]);
}

int ImportDataCache::getSuccess(const char * table)
{
    return success_cache_pos_[table].pos_;
}

void ImportDataCache::readFile(const char * file_name, Data &success_cache_pos, Datas& faild_datas)
{
	FILE *file = nullptr;
	fopen_s(&file, file_name, "rt");

	if (file == nullptr)
	{
		LOG(ERROR) << "Open file error";
		return;
	}

	char buf[256] = { 0 };

    if (fgets(buf, 256, file))
    {
		std::string temp = buf;

		StringReplace(temp, "/n", "");
		StringReplace(temp, "/r", "");
		StringReplace(temp, " ", "");

		auto index = temp.find_first_of(",-");

		if (index != std::string::npos)
		{
			success_cache_pos.field_index_ = temp.substr(0, index).c_str();
			success_cache_pos.pos_ = atoi(temp.substr(index + 1, temp.length() - index - 1).c_str());
		}
    }

	while (fgets(buf, 256, file) != nullptr)
	{
		std::string temp = buf;
		
		StringReplace(temp, "/n", "");
		StringReplace(temp, "/r", "");
		StringReplace(temp, " ", "");

		auto index = temp.find_first_of(",-");

		if (index != std::string::npos)
		{
			Data data;

			data.field_index_ = temp.substr(0, index).c_str();

			auto index1 = temp.find_last_of(",-");

			data.pos_ = atoi(temp.substr(index + 1, index1 - index - 1).c_str());
			data.length_ = atoi(temp.substr(index1 + 1, temp.length() - index1 - 1).c_str());

            faild_datas.push_back(data);
		}
		memset(buf, sizeof(buf), 0);
	};
	
	fclose(file);
}

void ImportDataCache::saveFile(const char * table, Data success_cache_pos, const Datas & datas)
{
    std::string full_name = "./temp/";
    full_name += table;
    full_name += ".import";

    FILE *file = nullptr;
    fopen_s(&file, full_name.c_str(), "wt");

    fprintf_s(file, "%s,%d", success_cache_pos.field_index_.c_str(), success_cache_pos.pos_);

    for (auto iter = datas.begin(); iter != datas.end(); ++iter)
    {
        fprintf_s(file, "\n%s,%d,%d", iter->field_index_.c_str(), iter->pos_, iter->length_);
    }

    if (file == nullptr)
    {
        LOG(ERROR) << "Open file error";
        return;
    }

    fclose(file);
}
